<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>添加库存申请单</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
	</head>
	<body>
		<form class="layui-form" action="" lay-filter="editform">
			<div class="mainBox">
				<div class="main-container">
					<div class="main-container">
						    <div class="layui-card">
						      <div class="layui-card-header">申请详情</div>
						      <div class="layui-card-body">
						        申请人Id : <span id="user_id"></span><br>
						        入库单Id : <span id="in_id"></span><br>
								商品名称 : <span id="goods_name"></span><br>
								商品型号 : <span id="model"></span><br>
								商品编号 : <span id="in_no"></span><br>
								商品库存 : <span id="stock_num"></span><br>
						      </div>
						    </div>
						<div class="layui-form-item">
							<label class="layui-form-label">申请数量</label>
							<div class="layui-input-block">
								<input type="number" name="num" lay-verify="required|len" autocomplete="off" min="1"
									placeholder="请输入申请数量" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-form-text">
							<label class="layui-form-label">备注</label>
							<div class="layui-input-block">
								<textarea name="desc" placeholder="请输入备注" class="layui-textarea"></textarea>
							</div>
						</div>

					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<input type="hidden" name="id" />
					<input type="hidden" name="user_id"/>
					<input type="hidden" name="in_id"/>
					<input type="hidden" name="stock_num">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="stock_apply-save">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
				</div>
			</div>
		</form>
		<script src="../config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['form', 'layer', 'tree'], function() {
				let form = layui.form,
					layer = layui.layer,
					$ = layui.$,
					req = _req();
				form.verify({
					len: (val, item) => {
						let min = item.getAttribute("min")
						if (val.length < min) {
							return '长度不能小于' + min;
						}
					}
				})
				if (req.id) {
					//渲染
					_get(layui, 'stock_apply/editBefore?id=' + req.id, res => {
						form.val('editform', res)
					});
				} else {
					_get(layui, 'stock_apply/addBefore?id=' + req.in_id + '&user_id=' + req.user_id, res => {
						//console.log(res)
						form.val('editform', res);
						['user_id','in_id','goods_name','model','in_no','stock_num'].forEach(d => {
							$('#' + d).text(res[d])
						})
					});
				}

				//监听提交
				form.on('submit(stock_apply-save)', function(data) {
					data = data.field;
					
					let postUrl;
					if (req.id) {
						postUrl = 'stock_apply/edit';
					} else {
						console.log(data)
						if(data.stock_num*1 < data.num*1) {
							layer.msg('申请数量不能大于库存数量')
							return false;
						}
						postUrl = 'stock_apply/add';
					}
					_post(layui, postUrl, data, res => {
						//console.log(res)
						parent.parent.layui.table.reload("stock_apply-table");
						parent.layer.closeAll(); //关闭当前页
						parent.parent.layer.closeAll();

					})

					return false;
				});

			});
		</script>

	</body>
</html>
